package com.yunshipei.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yunshipei.common.manager.EnterConfig;
import com.yunshipei.enterplorer.R;
import com.yunshipei.utils.BaseUtil;

public class CommonDialog extends Dialog {
    public OnClickListener listener;
    protected View btnContainer;
    protected FrameLayout container;
    private final int contentPadding;
    private TextView dialogTitle;

    protected TextView negativeBt;
    protected TextView positiveBt;
    protected OnClickListener dismissClick = new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    };

    public CommonDialog(Context context) {
        this(context, R.style.dialog_common);
    }

    public CommonDialog(Context context, int defStyle) {
        super(context, defStyle);
        contentPadding = (int) getContext().getResources().getDimension(R.dimen.global_dialog_padding);
        init(context);
    }

    protected CommonDialog(Context context, boolean flag, OnCancelListener listener) {
        super(context, flag, listener);
        contentPadding = (int) getContext().getResources().getDimension(R.dimen.global_dialog_padding);
        init(context);
    }

    @SuppressLint("InflateParams")
    protected void init(final Context context) {
        setCancelable(false);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View content = LayoutInflater.from(context).inflate(R.layout.dialog_common, null);
        dialogTitle = (TextView) content.findViewById(R.id.dialog_title);
        container = (FrameLayout) content.findViewById(R.id.content_container);
        btnContainer = content.findViewById(R.id.button_container);
        positiveBt = (TextView) content.findViewById(R.id.positive_bt);
        negativeBt = (TextView) content.findViewById(R.id.negative_bt);
        positiveBt.setTextColor(EnterConfig.getInstance().getGlobalColor());
        setContentView(content);

        Window window = this.getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.width = (int) (BaseUtil.getStandSize(context) * 0.8); //取最小值乘以80%
            window.setAttributes(params);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.dismiss();
    }

    public void setContent(View view) {
        setContent(view, contentPadding);
    }

    public void setContent(View view, int padding) {
        setContent(view, padding, padding, padding, padding);
    }

    public void setContent(View view, int left, int top, int right, int bottom) {
        container.removeAllViews();
        container.setPadding(left, top, right, bottom);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        container.addView(view, lp);
    }

    public void setMessage(int resId) {
        setMessage(getContext().getResources().getString(resId));
    }

    public void setMessage(Spanned spanned) {
        ScrollView scrollView = new ScrollView(getContext());
        scrollView.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT));
        TextView tvMessage = new TextView(getContext(), null,
                R.style.dialog_pinterest_text);
        tvMessage.setLayoutParams(new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT));
        tvMessage.setPadding(contentPadding, contentPadding, contentPadding, contentPadding);
        tvMessage.setLineSpacing(0.0F, 1.3F);
        tvMessage.setText(spanned);
        tvMessage.setTextColor(getContext().getResources().getColor(R.color.color_systemdialogitem));
        ScrollView.LayoutParams lp = new ScrollView.LayoutParams(
                ScrollView.LayoutParams.MATCH_PARENT,
                ScrollView.LayoutParams.WRAP_CONTENT);
        scrollView.addView(tvMessage, lp);
        setContent(scrollView, 0);
    }

    public void setMessage(String message) {
        setMessage(Html.fromHtml(message));
    }

    public void setNegativeButton(String negative, final OnClickListener listener) {
        if (!TextUtils.isEmpty(negative)) {
            negativeBt.setText(negative);
            negativeBt.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (listener != null)
                        listener.onClick(CommonDialog.this, 0);
                    else
                        dismissClick.onClick(CommonDialog.this, 0);
                }
            });
            negativeBt.setVisibility(View.VISIBLE);
        } else {
            negativeBt.setVisibility(View.GONE);
        }
    }

    public void setPositiveButton(String positive, final OnClickListener listener) {
        if (!TextUtils.isEmpty(positive)) {
            positiveBt.setText(positive);
            positiveBt.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        listener.onClick(CommonDialog.this, 0);
                    } else {
                        dismissClick.onClick(CommonDialog.this, 0);
                    }
                }
            });
            positiveBt.setVisibility(View.VISIBLE);
        } else {
            positiveBt.setVisibility(View.GONE);
        }
    }

    public void hideAllButton() {
        btnContainer.setVisibility(View.GONE);
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            dialogTitle.setText(title);
            dialogTitle.setVisibility(View.VISIBLE);
        } else {
            dialogTitle.setVisibility(View.GONE);
        }
    }
}
